package com.gotloop.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.google.appengine.api.datastore.Key;
import com.gotloop.jdo.Comment;

/**
 * Comment Data Access Object implementation.
 * @author jibhaine
 *
 */
@Repository
public class CommentDAOImpl extends AbstractDAOImpl<Comment, Key> implements CommentDAO {
	
	/**
	 * Logger for CommentDAOImpl.
	 */
	private static final Logger LOG = LoggerFactory.getLogger(CommentDAOImpl.class);

	@SuppressWarnings("unchecked")
	@Override
	public List<Comment> getAllLoopComments(int startIndex, int commentsPerPage) {
		LOG.debug("getAllLoopComments");
		
		List<Comment> commentList = null;
		PersistenceManager manager = getPersistenceManager();
		try {
			Query query = manager.newQuery(getEntityClass());
			query.setRange(startIndex, startIndex + commentsPerPage);
			commentList = (List<Comment>) manager.detachCopyAll((List<Comment>) query.execute());
		} catch (Exception exp) {
			LOG.error("Exception {}", exp.getLocalizedMessage());
		} finally {
			if (manager.currentTransaction().isActive()) {
				manager.currentTransaction().commit();
			}
			manager.close();
		}
		LOG.trace("getAllLoopComments : {}", commentList);
		return commentList;
	}

	@Override
	public List<Comment> getCommentsByLoops(String urlTitle, int startIndex, int commentsPerPage) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Comment> getCommentsByUser(String userName, int startIndex,	int commentsPerPage) {
		// TODO Auto-generated method stub
		return null;
	}


}
